package nju.jun.reader.tieba.data;

import java.io.File;

import nju.jun.reader.tieba.model.TiebaNovel;

public class TiebaNovelLoader implements TiebaNovelOnLoadListener {

	@Override
	public void onLoad(TiebaNovel novel) {
		if (null == novel) {
			return;
		}
		
		File dir = new File(novel.getStorageDirectory());
		if (!dir.exists() || !dir.isDirectory()) {
			return;
		}
		
		int maxVol = -1;
		File maxVolDir = null;
		File[] volumeDirs = dir.listFiles();
		for (File volumeDir : volumeDirs) {
			if (volumeDir.isDirectory()) {
				int volNo = parseVolumeDir(volumeDir);
				if (volNo > maxVol) {
					maxVol = volNo;
					maxVolDir = volumeDir;
				}
			}
		}
		if (null == maxVolDir) {
			novel.setVolume(0);
		}
		// TODO: fill this method in
	}
	
	private int parseVolumeDir(File volDir) {
		String name = volDir.getName();
		String intStr = name.substring(0, name.indexOf("_"));
		int rtn = -1;
		try {
			rtn = Integer.parseInt(intStr);
		} catch (NumberFormatException e) {
			
		}
		return rtn;
	}
	
}
